package net.sea.setup.ui.wizard;

import java.awt.CardLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.apache.log4j.Logger;

import net.sea.commons.constants.AppConstants;
import net.sea.commons.context.AppContext;
import net.sea.commons.util.StringUtils;
import net.sea.setup.config.Menu;
import net.sea.setup.config.Page;
import net.sea.setup.config.Wizard;
import net.sea.setup.ui.wizard.IWizardPage.PageResult;

/**
 * 向导容器页面
 * 
 * @author 华成伍
 *
 */
@SuppressWarnings("serial")
public class WizardPanel extends JPanel {
	private Logger logger = Logger.getLogger(this.getClass());
	/**
	 * 第一页
	 */
	public final static int FIRST_PAGE = 1;
	/**
	 * 中间页
	 */
	public final static int MIDDLE_PAGE = 2;
	/**
	 * 尾页
	 */
	public final static int LAST_PAGE = 3;
	private CardLayout cardLayout = new CardLayout(0, 0);
	// 当前显示的页码
	private static int currentPageIndex = 1;
	// 所有页面
	private List<AbstractWizardPage> pagePanels = new ArrayList<AbstractWizardPage>();
	// 切换按钮
	private List<JButton> buttons = new ArrayList<JButton>();
	// 获取上下文实例对象
	private static AppContext contextInst = AppContext.getInstance();

	/**
	 * 创建向导panel
	 */
	public WizardPanel() {
		setLayout(cardLayout);
		// 获取系统的配置
		Wizard wizard = (Wizard) contextInst
				.getAttribute(AppConstants.CFG_WIZARD_KEY);
		if (wizard == null) {
			logger.error("系统配置错误……");
			throw new RuntimeException("系统配置错误……");
		}
		List<Page> pages = wizard.getPages();
		for (Page page : pages) {
			try {
				AbstractWizardPage pagePanel = (AbstractWizardPage) (Class
						.forName(page.getPanelClass()).newInstance());
				pagePanel.setParent(this);
				pagePanels.add(pagePanel);
				// 把向导页面的panel存储到系统上下文中
				contextInst.setAttribute(AppConstants.WIZARD_PAGES_KEY,
						pagePanels);
				addWizardPage(pagePanel);
			} catch (Exception e) {
				logger.error("页面初始化异常", e);
				throw new RuntimeException(e);
			}
		}
	}

	/**
	 * 获取切换按钮
	 * 
	 * @return
	 */
	public List<JButton> getButtons() {
		return buttons;
	}

	/**
	 * 添加按钮
	 * 
	 * @param button
	 */
	public void addButton(JButton button) {
		buttons.add(button);
	}

	/**
	 * 添加向导页
	 * 
	 * @param page
	 *            向导页
	 */
	public void addWizardPage(AbstractWizardPage page) {
		add(page, page.getIndex() + "");
	}

	/**
	 * 下一页
	 */
	public int next() {
		int index = currentPageIndex - 1 < 0 ? 0 : currentPageIndex - 1;
		AbstractWizardPage wizardPage = pagePanels.get(index);
		// 判断页面验证是否填写正确
		PageResult result = wizardPage.validateInput();
		if (result.isPassed()) {
			cardLayout.next(this);
			currentPageIndex++;
			setMenu();
			// 页面处理完成的业务处理
			wizardPage.done();
			pagePanels.get(index + 1).ready();
			return currentPageIndex < getComponentCount() ? MIDDLE_PAGE
					: LAST_PAGE;
		} else {
			wizardPage.done();
			String message = result.getMessage();
			if (StringUtils.isNotEmpty(message)) {
				JOptionPane.showMessageDialog(getParent(), message,
						AppConstants.MESSAGE_DIALOG_TITLE,
						JOptionPane.WARNING_MESSAGE);
			}
			return currentPageIndex == 1 ? FIRST_PAGE
					: currentPageIndex == getComponentCount() ? LAST_PAGE
							: MIDDLE_PAGE;
		}
	}

	/**
	 * 设置菜单的背景色
	 */
	@SuppressWarnings("unchecked")
	private void setMenu() {
		List<JLabel> lables = (List<JLabel>) contextInst
				.getAttribute(AppConstants.WIZARD_MENU_KEY);
		if (lables != null) {
			for (int i = 0; i < lables.size(); i++) {
				JLabel lb = lables.get(i);
				if (i < currentPageIndex - 1) {
					lb.setIcon(new ImageIcon(Menu.class
							.getResource("/img/installed.gif")));
				} else if (i == currentPageIndex - 1) {
					lb.setIcon(new ImageIcon(Menu.class
							.getResource("/img/installing.gif")));
				} else {
					lb.setIcon(null);
				}
			}
		}
	}

	/**
	 * 上一页
	 */
	public int previous() {
		cardLayout.previous(this);
		currentPageIndex--;
		setMenu();
		return currentPageIndex > 1 ? MIDDLE_PAGE : FIRST_PAGE;
	}

}
